class AccountsController < ApplicationController
  
  before_filter :authorize
  
  layout :determine_layout
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @account_pages, @accounts = paginate :accounts, :per_page => 10
  end

  def show
    @account = Account.find(params[:id])
  end

  def new
    @account = Account.new
    if params[:id]
      session[:is_person] = true
    else
      session[:is_person] = false
    end
  end

  def create
    @billing = Address.new(params[:billing_address])
    @shipping = Address.new(params[:shipping_address])
    if session[:is_person]
      @party = Person.new(params[:customer])
    else
      @party = Company.new((params[:customer]))
    end
    
    begin
    Party.transaction do
      @party.save!
      
      @billing.save! unless @billing.street.blank?
      @shipping.save! unless @shipping.street.blank?
      @account = Account.new
      @account.billing_address = @billing unless @billing.street.blank?
      @account.shipping_address = @shipping unless @shipping.street.blank?
      @account.customer = @party
      
      @account.save!
      
      session[:is_person] = nil
      
      flash[:notice] = 'Account was successfully created.'
      redirect_to :action => 'list'
    end
    
    rescue ActiveRecord::RecordInvalid => e
      @billing.valid?
      @shipping.valid?
      @account.valid?
      render :action => 'new'
    end
  end

  def edit
    @account          = Account.find(params[:id])
    @customer         = @account.customer
    @billing_address  = @account.billing_address
    @shipping_address = @account.shipping_address
    
    if @customer.class == Person
      session[:is_person] = true
    else
      session[:is_person] = false
    end
      
  end

  def update
    @account = Account.find(params[:id])
    
    begin      
      @account.customer.update_attributes(params[:customer])
      
      @account.customer.save!
      
      if @account.billing_address.nil?
         @account.billing_address = Address.new(params[:billing_address])
         @account.billing_address.save! unless @account.billing_address.street.blank?
      else
        @account.billing_address.update_attributes(params[:billing_address])
      end
      
      @account.billing_address.save! unless @account.billing_address.nil?
      
      if @account.shipping_address.nil?
        @account.shipping_address = Address.new(params[:shipping_address])
        @account.shipping_address.save! unless @account.shipping_address.street.blank?
      else
        @account.shipping_address.update_attributes(params[:shipping_address])
      end
      
      @account.shipping_address.save! unless @account.shipping_address.nil?
      
      session[:is_person] = nil
    
      @account.save!
      flash[:notice] = 'Account was successfully updated.'
      redirect_to :action => 'show', :id => @account
    
    rescue ActiveRecord::RecordInvalid => e
      @account.customer.valid?
      @account.billing_address.valid?
      @account.shipping_address.valid?
      render :action => 'edit'
    end
  end

  def destroy
    Account.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
